<template>
  <el-form ref="userRef" :model="user" :rules="rules" label-width="auto">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="user.name" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="user.mobile" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="user.email" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="user.gender">
        <el-radio v-for="(item,index) in getEnumList('UserGender')" :label="item.key" :key="index">{{item.value}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idcard">
      <el-input v-model="user.idcard" maxlength="18" />
    </el-form-item>
    <el-form-item label="最后一次登录时间" prop="lastLoginTime">
      <span style="color: var(--el-input-text-color,var(--el-text-color-regular));">{{ user.lastLoginTime || '无' }}</span>
    </el-form-item>
    <el-form-item label="最后一次登录IP" prop="lastLoginIp">
      <span style="color: var(--el-input-text-color,var(--el-text-color-regular));">{{ user.lastLoginIp || '未知' }}</span>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit('userRef')">保存</el-button>
      <el-button @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {getCurrentInstance, ref} from "vue";
import {doPost} from "@/config/http";
import {alertErrorMsg, getEnumList, svc} from '@/config/common'

const props = defineProps({
  user: {
    name: '',
    mobile: '',
    email: '',
    gender: '',
    idcard: '',
  }
});

const {proxy} = getCurrentInstance();

const rules = ref({
  name: [{required: true, message: "姓名不能为空", trigger: "blur"}],
  email: [
    {required: true, message: "邮箱地址不能为空", trigger: "blur"},
    {type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"]}
  ],
  mobile: [
    {required: true, message: "手机号码不能为空", trigger: "blur"},
    {pattern: /^1[3456789][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: ["blur"]}
  ],
  gender: [{required: false, message: "请选择性别", trigger: "blur"}],
  idcard: [{required: false, message: "请输入身份证号码", trigger: "blur"}],
});

/** 提交按钮 */
async function submit(formName) {
  let v = await proxy.$refs[formName].validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  doPost(svc, '/custom/updateProfile', props.user).then(() => {
    proxy.$modal.msgSuccess("修改成功");
  })
}

/** 关闭按钮 */
function close() {
  proxy.$tab.closePage();
}
</script>
